//
//  ChannelHotPostsTableViewCell.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/13.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ChannelHotPostsTableViewCell.h"
#import "ChannelPresentationCellFrameViewModel.h"
#import "ChannelPostModel.h"
#import "ChannelPostContent.h"
#import "ChannelPostHotposts.h"
#import "ChannelPosts.h"
#import "ChannelPostImg.h"
#import "ChannelVoice.h"


#import "TAMacro.h"
#import "ChannelPhotoCollectionViewCell.h"
#import "ChannelVoiceCollectionViewCell.h"
#import "UIImageView+WebCache.h"
@interface ChannelHotPostsTableViewCell ()
<
    UICollectionViewDelegate,
    UICollectionViewDataSource,
    UICollectionViewDelegateFlowLayout
>
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UILabel *hotPostsTitleLabel;
@property (nonatomic, strong) UILabel *allLabel;
@property (nonatomic, strong) UICollectionView *photoCollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@end

@implementation ChannelHotPostsTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 所有子控件的创建
        self.contentView.backgroundColor = [UIColor colorWithWhite:0.917 alpha:1];
        [self setUpAllChildrenView];
        
    }
    return self;
}

- (void)setUpAllChildrenView {
    // 背景图
    _backView = [[UIView alloc] init];
    _backView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_backView];
    // 标题
    _hotPostsTitleLabel = [[UILabel alloc] init];
//    _hotPostsTitleLabel.backgroundColor = [UIColor cyanColor];
    _hotPostsTitleLabel.font = TitleFont;
    [_backView addSubview:_hotPostsTitleLabel];
    
    // 图片
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.minimumLineSpacing = 2;
    _layout.minimumInteritemSpacing = 1.5;
    _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);


    
    _photoCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    _photoCollectionView.backgroundColor = [UIColor whiteColor];
    _photoCollectionView.dataSource = self;
    _photoCollectionView.delegate = self;


    [_backView addSubview:_photoCollectionView];

    [_photoCollectionView registerNib:[UINib nibWithNibName:@"ChannelPhotoCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ChannelPhotoCollectionViewCellReuse"];
    [_photoCollectionView registerNib:[UINib nibWithNibName:@"ChannelVoiceCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ChannelVoiceCollectionViewCellReuse"];
    // 查看所有
    _allLabel = [[UILabel alloc] init];
    _allLabel.text = @"查看更多 >";
    _allLabel.textAlignment = 1;
    _allLabel.textColor = [UIColor lightGrayColor];
    _allLabel.font = TextFont;
//    _allLabel.backgroundColor = [UIColor yellowColor];
    [_backView addSubview: _allLabel];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return _channelPresentationCellFrameViewModel.itemSizeArray.count;
//    return 6;
}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *sizeString = _channelPresentationCellFrameViewModel.itemSizeArray[indexPath.row];
    CGSize cellSize = CGSizeFromString(sizeString);
    return cellSize;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([_channelPresentationCellFrameViewModel.channelPost.content.hotPosts.posts[indexPath.row].postType isEqualToString:@"Voice"]) {
        ChannelVoiceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ChannelVoiceCollectionViewCellReuse" forIndexPath:indexPath];
        cell.voice = _channelPresentationCellFrameViewModel.channelPost.content.hotPosts.posts[indexPath.row].voice;
        return cell;
    }
    ChannelPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ChannelPhotoCollectionViewCellReuse" forIndexPath:indexPath];
    [cell.photoImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://qn.zheli.io/%@", _channelPresentationCellFrameViewModel.channelPost.content.hotPosts.posts[indexPath.row].img.firstObject.f]]];
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *ID = _channelPresentationCellFrameViewModel.channelPost.content.hotPosts.posts[indexPath.row].ID;
    self.postDetailPage(ID);
}


- (void)setChannelPresentationCellFrameViewModel:(ChannelPresentationCellFrameViewModel *)channelPresentationCellFrameViewModel {
    
    _channelPresentationCellFrameViewModel = channelPresentationCellFrameViewModel;
    // 处理数据
    [self setUpData];
    // 处理frame
    [self setUpFrame];
    [self.photoCollectionView reloadData];
}

// 处理数据
- (void)setUpData {
    _hotPostsTitleLabel.text = _channelPresentationCellFrameViewModel.channelPost.content.hotPosts.countDesc;
    
    
}
// 处理frame
- (void)setUpFrame {
    _backView.frame = _channelPresentationCellFrameViewModel.hotPostBackViewFrame;
    _photoCollectionView.frame = _channelPresentationCellFrameViewModel.imageCollectionViewFrame;
    _photoCollectionView.contentSize = CGSizeMake(SCREEN_WIDTH - 40, CGRectGetHeight(_photoCollectionView.frame));
    _hotPostsTitleLabel.frame = _channelPresentationCellFrameViewModel.titleLabelFrame;
    _allLabel.frame = _channelPresentationCellFrameViewModel.allLabelFrame;
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
